package 临时工具.shangyu2;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.digitenjoy.common.core.utils.GPSDataConvertUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import 临时工具.ExportExcel;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
import java.util.ArrayList;

@Data
public class wsbz implements Serializable {
    //读取文件
    static String filePath = "C:\\Users\\瑾瑜\\Desktop\\sql\\front\\source\\gw\\wsbz.geojson";
    static String outPath = "C:\\Users\\瑾瑜\\Desktop\\sql\\front\\source\\gw\\wsbz_out.xlsx";
    private static final ArrayList<String> propertiesList = new ArrayList<String>(){{

//        add("WTDH");
//        add("X");
//        add("Y");
//        add("FSW");
//        add("SSXZJD");
        //        add("PCDW" );
        //        add("BZMC" );

        add("SSPQ");
        add("BZ");
        add("clearname");
        add("jd02" );
        add("wd02" );
        add("jd84" );
        add("wd84" );

    }};
    public static void main(String[] args) throws IOException {

        String content = FileUtil.readUtf8String(filePath);
        JSONObject jsonObject = JSONObject.parseObject(content);
        JSONArray features = jsonObject.getJSONArray("features");

        JSONArray list = new JSONArray();
        for (int i = 0; i < features.size(); i++) {
            JSONObject feature = features.getJSONObject(i);
            JSONObject properties = feature.getJSONObject("properties");
            JSONArray coordinates = feature.getJSONObject("geometry").getJSONArray("coordinates");
            Double start = coordinates.getDouble(0);
            Double end = coordinates.getDouble(1);
            properties.put("jd84", start);
            properties.put("wd84", end);

            double[] doubles1 = GPSDataConvertUtil.transformWGS84ToGCJ02(start, end);
            properties.put("jd02", String.valueOf(doubles1[0]));
            properties.put("wd02", String.valueOf(doubles1[1]));

            JSONObject object = new JSONObject();
            for (String key:properties.keySet()) {
                if (propertiesList.contains(key)) {
                    object.put(key.toLowerCase(), properties.getString(key));
                }
                if ("BZ".equals(key)) {
                    object.put("clearname", clearName(properties.getString("BZ")));
                }
            }
//            if ("百官所".equals(object.getString("sspq")))
                list.add(object);
        }

//       FileUtil.writeUtf8String(list.toJSONString(), outPath);
         System.out.println(list.toJSONString());
        ex(list);


    }
    private static void ex(JSONArray list ) throws IOException {
        ExportExcel exportExcel = new ExportExcel();

        ArrayList<String> arr = propertiesList;

        for(int j = 0; j < arr.size(); j++){
            exportExcel.setValue(j, arr.get(j).toLowerCase());
        }
        exportExcel.addRowIndex();
        for(int i = 0; i < list.size(); i++){
            JSONObject obj = list.getJSONObject(i);
            for(int j = 0; j < arr.size(); j++){

                exportExcel.setValue(j, obj.getString(arr.get(j).toLowerCase()));

            }
            exportExcel.addRowIndex();
        }
        exportExcel.write(Files.newOutputStream(new File(outPath).toPath()));
    }

    private static String clearName(String name) {
        if(StringUtils.isNotBlank(name)){
            return name.replaceFirst("^\\d+", "");
        }else{
            return "";
        }
    }

}
